home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 777 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: gate.net!pslfl2-47
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: gets() question
  5. Date: 9 Jan 1996 04:01:31 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4cspar$1rdi@news.gate.net>
  8. References: <4cosgf$rir@newsbf02.news.aol.com> <4cqkt8$1quo@news.gate.net> <821109116snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: pslfl2-47.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <821109116snz@genesis.demon.co.uk>,
  13.    Lawrence Kirby <fred@genesis.demon.co.uk> wrote:
  14. >In article <4cqkt8$1quo@news.gate.net> bhutto@gate.net "William Hutto" 
  15. writes:
  16. >
  17. >>In article <4cosgf$rir@newsbf02.news.aol.com>,
  18. >
  19. >>>#include "stdio.h"
  20. >
  21. >Should be:
  22. >
  23. >#include <stdio.h>
  24. >
  25. >if you want to be sure to include the standard system stdio.h header.
  26. >
  27. >>>void main(void)
  28. >
  29. >Read the FAQ.
  30. >
  31. >>>{
  32. >>>   int i[5];
  33. >>>   char s[3][10];
  34. >>>
  35. >>>   printf ("Enter s[0]:  ");
  36. >>>   gets (s[0]);
  37. >>          ^^^^          
  38. >>
  39. >>Your compiler didn't generate an error?
  40. >
  41. >Why should it?
  42. >
  43. >>*s* is not an array of pointers to 
  44. >>arrays of chars as in argv[].
  45. >
  46. >True, s is an array of arrays of char. s[0] is an array of 10 chars.
  47. >When used as an rvalue in an expression an array evaluates to a pointer to
  48. >its first element i.e. a char * in this case.
  49. >
  50. >>Now I remember why I always like being explicit. 
  51. >>Try this:
  52. >>
  53. >>        fgets(&s[0][0],9,stdin);
  54. >
  55. >It is certainly better to use fgets rather than gets but you could have
  56. >written:
  57. >
  58. >         fgets(s[0],sizeof s[0],stdin);
  59. >
  60. >s[0] is equivalent to &s[0][0] in this context and is arguably more
  61. >readable. The length argument of fgets specifies the size of the array
  62. >available in the first argument (which is 10 in this case) i.e. fgets will
  63. >read a maximum of N-1 characters from the input stream and then terminate the
  64. >sequence in the array with '\0'.
  65. >
  66. >>You can use a 2 dimensional char array, except that you have to be complete 
  67. >>in your form and s[0] is not. Because fgets() (or gets() for that matter) is 
  68. >>looking for an address, you need to oblige, hence the prefix of *&*.
  69. >
  70. >You need to read up on how arrays and pointers work in C. There are no such
  71. >thing as 2 dimensional arrays, just arrays of arrays. Each element of the
  72. >overall array is itself an array and behaves just like any other array.
  73. >
  74.  
  75. One thing I liked about C was it's predictability. :) That was one little 
  76. quirk that I didn't remember. I tend to be explicit in the use of subscripts 
  77. or dereferencing, and so if I had knowledge of that, it must have slipped into 
  78. the bit bucket. I appreciate the clarification guys, and sorry for the 
  79. erroneous data. 
  80.  
  81. Bill
  82.  
  83. "Whatcha got on?...Your mind?"
  84.